package com.jawmask.android.stuartproair.ui;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.jawmask.android.stuartproair.R;
import com.jawmask.android.stuartproair.constants.AppConstants;

public class WebBrowserActivity extends BaseActivity {

	private WebView webBrowserView;
	private ProgressBar webViewLoadingBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web_view_layout, true);

		Intent intent = getIntent();
		String urlToLoad = intent.getStringExtra(AppConstants.EXTRA_KEY_NAME_URL_TO_LOAD);
		String title = intent.getStringExtra(AppConstants.EXTRA_KEY_NAME_BROWSER_TITLE_NAME);

		setTitle(title);

		webViewLoadingBar = (ProgressBar) findViewById(R.id.webViewLoadingBar);

		webBrowserView = (WebView) findViewById(R.id.webBrowserView);
		webBrowserView.getSettings().setJavaScriptEnabled(true);
		webBrowserView.setWebChromeClient(paymentFlowLoadingBarClient);

		webBrowserView.loadUrl(urlToLoad);
	}

	private WebChromeClient paymentFlowLoadingBarClient = new WebChromeClient() {
		public void onProgressChanged(WebView view, int progress) {
			if (progress < 100 && webViewLoadingBar.getVisibility() == ProgressBar.GONE) {
				webViewLoadingBar.setVisibility(ProgressBar.VISIBLE);
			}
			webViewLoadingBar.setProgress(progress);
			if (progress == 100) {
				webViewLoadingBar.setVisibility(ProgressBar.GONE);
			}
		}
	};
}
